package cn.com.xy.sms.sdk.service.validcode;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.dex.DexUtil;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.service.baseparse.BaseParseService;
import cn.com.xy.sms.sdk.util.StringUtils;
import cn.com.xy.sms.util.ParseBubbleManager;

/**
 * 验证码服务
 * @author hewengao
 *
 */
public class ValidCodeService {
	 /**
     * 解析验证码短信
     * 
     * @param number
     *            短信发送者号码
     * @param smsBody
     *            短信内容
     * @param receiveTime
     *            短信接收时间
     * @return MapKey: isValidCodeSms=(boolean)是否验证码短信 validTime=(long)
     *         验证码有效时长的时间戳,为-1时代表有异常,为0代表短信内缺少有效时长关键字
     *         parseResult=(String)解析结果,为"succeed"代表获取验证码有效时长过程无异常，其它值则为异常说明
     */
    public static Map<String, Object> parseValidCodeSms(String number, String smsBody, long receiveTime) {
        if (StringUtils.isNull(number)) {
            return createParseValidCodeResultMap(false, -1L, "error:number is null or empty");
        }
        if (StringUtils.isNull(smsBody)) {
            return createParseValidCodeResultMap(false, -1L, "error:smsBody is null or empty");
        }
        if (receiveTime <= 0) {
            return createParseValidCodeResultMap(false, -1L, "error:receiveTime <= 0");
        }

        try {
            Map<String, String> extend = new HashMap<String, String>();
            extend.put("popup_type", "2");
            extend.put("parseVerifyCode", "true");
            Map<String, Object> parseResultMap =  BaseParseService.parseMsg(Constant.getContext(), number, null, smsBody, receiveTime, extend);
            if (parseResultMap == null || ParseBubbleManager.getParseStatu(parseResultMap) == -1) {
                return createParseValidCodeResultMap(false, -1L, "error:parse sms failure");
            }

            int result = DexUtil.getSmsTypeByMap(parseResultMap, 1);
            if (result == Constant.SMS_TYPE_CODE) {
                parseResultMap.put("smsReceiveTime", receiveTime);
                Map<String, Object> validTimeMap = DexUtil.handleValidTime(parseResultMap);
                if (validTimeMap == null || validTimeMap.get("validTime") == null || "-1".equals(String.valueOf(validTimeMap.get("validTime")))) {
                    return createParseValidCodeResultMap(true, -1, "error:get valid time failure");
                } else {
                    return createParseValidCodeResultMap(true, (Long) validTimeMap.get("validTime"), "succeed");
                }
            }

            return createParseValidCodeResultMap(false, -1L, "not a valid code sms");
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.e(Constant.TAG, "parseValidCodeSms: ", e);

            return createParseValidCodeResultMap(false, -1L, "error:" + e.getMessage());
        }
    }

    private static Map<String, Object> createParseValidCodeResultMap(boolean isValidCodeSms, long validTime, String parseMessage) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isValidCodeSms", isValidCodeSms);
        map.put("validTime", validTime);
        map.put("parseResult", parseMessage);
        return map;
    }

    /**
     * 判断是否为验证码短信
     * 
     * @param ctx
     *            上下文
     * @param phoneNumber
     *            短信发送者号码
     * @param smsContent
     *            短信内容
     * @param smsCenterNum
     *            短信中心号
     * @param extend
     *            拓展参数
     * @return
     */
    public static boolean isVerifyCodeSms(Context ctx, String phoneNumber, String smsContent, String smsCenterNum, Map<String, String> extend) {
        try {
            if (extend == null) {
                extend = new HashMap<String, String>();
            }
            extend.put("popup_type", "2");
            extend.put("parseVerifyCode", "true");
            Map<String, Object> resultMap =  BaseParseService.parseMsg(ctx, phoneNumber, smsCenterNum, smsContent, 0l, extend);
            if (resultMap != null && ParseBubbleManager.getParseStatu(resultMap) != -1) {
                return DexUtil.getSmsTypeByMap(resultMap, Constant.SMS_TYPE_CODE) == Constant.SMS_TYPE_CODE;
            }
        } catch (Throwable e) {
            // e.printStackTrace();
            LogManager.e(Constant.TAG, "isVerifyCodeSms: ", e);

        }
        return false;
    }
}
